package edu.codink.xq.center.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import edu.codink.xq.center.entity.Userinfo;

@Service
@SuppressWarnings("unchecked")
public class UserDao
{
	private static final Logger LOGGER = Logger.getLogger(UserDao.class);

	public static final String LOGIN_FLAG = "login";
	public static final String CURRENT_USER = "current_user";
	public static final String RULE = "rule";
	@Autowired
	private HibernateDao hibernateDAO;

	public Map<String, Object> login(String username, String password)
	{
		// 返回结果容器
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 检查用户是否存在
		List<Userinfo> userList = hibernateDAO.findByProperty(Userinfo.class,
				new String[] { "loginname", "password" }, new String[] {
						username, password });
		// 如果没有对应的用户，结果容器中加入登录失败的标志
		if (userList == null || userList.size() == 0)
		{
			LOGGER.debug("用户名:" + username + " 密码:" + password
					+ " 在用户表中没有找到对应的用户,登录失败");
			resultMap.put(LOGIN_FLAG, false);
		}
		// 有对应的用户，登录成功
		else
		{
			resultMap.put(LOGIN_FLAG, true);
		}
		return resultMap;
	}

	public List<Userinfo> getLoginUser(String userName, String password)
	{

		String hql = "FROM Userinfo WHERE loginName='" + userName
				+ "' AND password='" + password + "'";
		List<Userinfo> users = this.hibernateDAO.find(hql);
		return users;
	}
}
